﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using EzDesk.Core.Model;
using System.Text;

namespace EzDesk.Apps.Model.Attendance
{
    /// <summary>
    /// 描述一天的出勤考勤情况
    /// </summary>
    public partial class DayAttendanceEvaluation
    {
        private bool _late;
        private bool _isLeaveEarly;
        private string _status;

        public DayAttendanceEvaluation()
        {
            Id = Guid.Empty;
            LeaveInfo = new DayLeaveInfo();
        }

        [DataMember]
        public DayLeaveInfo LeaveInfo { get; set; }

        /// <summary>
        /// 当日的考勤列表
        /// </summary>
        [DataMember]
        public List<AttendanceInfo> DayAttendanceInfoList { get; set; }

        /// <summary>
        /// 是否迟到
        /// </summary>
        [DataMember]
        public bool IsLate
        {
            get
            {
                return LateMiniutes > 0;
            }
            set { _late = value; }
        }


        /// <summary>
        /// 是否早退
        /// </summary>
        [DataMember]
        public bool IsLeaveEarly
        {
            get
            {
                return EarlyMiniutes > 0;
            }
            set { _isLeaveEarly = value; }
        }

        [DataMember]
        public string Status
        {
            get { return getEvaulationStatus(); }
            set { _status = value; }
        }

        private string getEvaulationStatus()
        {
            var status = "normal";

            if (LeaveInfo.HasLeave) status = "leave";

            if (!IsNeedToWork) status = "notwork";

            if (IsAbsense) status = "absence";
            return status;
        }
    }

    public static class DayEvaluationExtension
    {
        /// <summary>
        /// 将分钟为单位的工时转换为以小时为单位精确到0.5小时的工时
        /// </summary>
        /// <param name="miniutes"></param>
        /// <returns></returns>
        public static int ToManhours(this Int32 miniutes)
        {
            return miniutes / 6 / 5 * 5;
        }

        public static string GetCurrentApplicationString (this EvaluationModifyApplication applications)
        {
            var applicationStr = "";
            if (applications != null)
                applicationStr = "申请修改中";

            return applicationStr;
        }

        public static string GetLatestModifyHistroyString(this EvaluationModifyHistory modifyHistory)
        {
            var sb = new StringBuilder();
            if (modifyHistory != null)
                sb.AppendFormat("{0} 修改过", modifyHistory.Operator.Name);
            return sb.ToString();
        }

        public static string GetEvaluationStatusString(this DayAttendanceEvaluation evaluation)
        {
            var sb = new StringBuilder();
            sb.AppendFormat("工时 {0}小时 ", ((double)(evaluation.Manhours) / 10).ToString("#0.0"));
            
            if (evaluation.IsAbsense)
            {
                sb.Append("</br>");
                sb.Append(" 旷工");
            }
            if (evaluation.IsExtraWork)
            {
                sb.Append("</br>");
                sb.Append(" 加班");
            }
            if (evaluation.IsLate)
            {
                sb.Append("</br>");
                sb.AppendFormat(" 迟到 {0} 分钟", evaluation.LateMiniutes);
            }
            if (evaluation.IsLeaveEarly)
            {
                sb.Append("</br>");
                sb.AppendFormat(" 早退 {0} 分钟", evaluation.EarlyMiniutes);
            }
            if (!evaluation.IsNeedToWork)
            {
                sb.Append("</br>");
                sb.Append(" 休息");
            }
            return sb.ToString();
        }

        public static string GetSimpleEvaluationStatusString(this DayAttendanceEvaluation evaluation)
        {
            var sb = new StringBuilder();

            if (evaluation.IsAbsense)
            {
                sb.Append(" 旷工");
            }
            if (evaluation.IsExtraWork)
            {
                sb.Append(" 加班");
            }
            if (evaluation.IsLate)
            {
                sb.AppendFormat(" 迟到 {0} 分钟", evaluation.LateMiniutes);
            }
            if (evaluation.IsLeaveEarly)
            {
                sb.AppendFormat(" 早退 {0} 分钟", evaluation.EarlyMiniutes);
            }
            if (!evaluation.IsNeedToWork)
            {
                sb.Append(" 休息");
            }
            return sb.ToString();
        }



        public static string GetEvaluationStatusClassName(this DayAttendanceEvaluation evaluation)
        {
            if (evaluation.Date.Date == DateTime.Now.Date)
            {
                return "fc-event-today";
            }

            var className = "";
            switch (evaluation.Status)
            {
                case "normal" :
                    className = "fc-normal";
                    break;
                case "leave":
                    className = "fc-leave";
                    break;
                case "notwork":
                    className = "fc-holiday";
                    break;
                case "absence":
                    className = "fc-abnormal";
                    break;
                default:
                    className = "fc-normal";
                    break;
            }
            return className;
        }
    }
}